<!DOCTYPE html>
<html lang="en">

<head>
  <meta charset="UTF-8">
  <meta http-equiv="X-UA-Compatible" content="IE=edge">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>Document</title>
</head>

<body>
  <!-- 递归 -->
  <script>
    let arr = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10];
    const result = binarySearchSelf(arr, 0, arr.length - 1, 44);
    console.log(result);
    // value ——要查找的值,
    function binarySearchSelf(arr, minIndex, maxIndex, value) {
      if (minIndex > maxIndex) {
        return -1;
      }
      let midIndex = parseInt('' + (minIndex + maxIndex) / 2);
      if (value === arr[midIndex]) {
        return midIndex;
      } else if (value > arr[midIndex]) {
        return binarySearchSelf(arr, midIndex + 1, maxIndex, value);
      } else if (value < arr[midIndex]) {
        return binarySearchSelf(arr, minIndex, maxIndex - 1, value);
      }
    }
    
  </script>
</body>

</html>